package com.train.common.until;

import com.train.common.domin.Member;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MemberLoginContextUtil {
    private static final Logger LOG = LoggerFactory.getLogger(MemberLoginContextUtil.class);

    private static final ThreadLocal<Member> threadLocal = new ThreadLocal<>();

    /**
     * 获取登录用户的id
     * @return
     */
    public static Long getId(){
        try {
            return threadLocal.get().getId();
        } catch (Exception e) {
            LOG.error("获取登录会员信息异常", e);
            throw e;
        }
    }

    /**
     * 保存登录用户信息
     * @param member
     */
    public static void setMember(Member member){
        threadLocal.set(member);
        System.out.println("保存的会员id"+member.getId());
    }

    public static void clearMember(){
        threadLocal.remove();
    }
}
